package com.jianjun.ktui.ui.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Place
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.savedstate.SavedState
import androidx.savedstate.savedState
import androidx.savedstate.write
import coil3.compose.AsyncImage
import coil3.compose.LocalPlatformContext
import coil3.request.ImageRequest
import coil3.request.crossfade
import jianjun_gradle.composeapp.generated.resources.Res
import jianjun_gradle.composeapp.generated.resources.html_name
import jianjun_gradle.composeapp.generated.resources.login_background
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.ui.tooling.preview.Preview

/**
 *
 * @author source
 * {@code @date} 2025/3/21 14:01
 */

@Composable
fun LoginScreen(
    showSnackbar: (String) -> Unit,
    uiState: SavedState,
    imageUrl: String,
    onLoginClicked: () -> Unit,
    onCodeClicked: () -> Unit,
    toRegisterClicked: () -> Unit
) {
    var username by remember { mutableStateOf("admin") }
    var password by remember { mutableStateOf("admin123") }
    var code by remember { mutableStateOf("") }
    Box(
        modifier = Modifier
            .paint(painterResource(Res.drawable.login_background), contentScale = ContentScale.FillBounds),
        contentAlignment = Alignment.Center
    ) {
        Column(
            modifier = Modifier.clip(RoundedCornerShape(10.dp))
                .background(Color.White)
                .padding(20.dp)
                .width(400.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Text(
                stringResource(Res.string.html_name),
                modifier = Modifier.padding(bottom = 10.dp),
                style = MaterialTheme.typography.titleLarge,
                maxLines = 1,
                color = Color(0xFF707070),
                textAlign = TextAlign.Center
            )
            OutlinedTextField(
                value = username,
                modifier = Modifier.fillMaxWidth(),
                leadingIcon = {
                    Icon(Icons.Filled.AccountBox, null)
                },
                placeholder = {
                    Text("账号")
                },
                onValueChange = {
                    username = it
                },
                singleLine = true
            )
            Spacer(modifier = Modifier.height(8.dp))
            OutlinedTextField(
                value = password,
                modifier = Modifier.fillMaxWidth(),
                leadingIcon = {
                    Icon(Icons.Filled.Lock, null)
                },
                placeholder = {
                    Text("密码")
                },
                visualTransformation = PasswordVisualTransformation(),
                onValueChange = {
                    password = it
                },
                singleLine = true
            )
            Spacer(modifier = Modifier.height(8.dp))
            Row(verticalAlignment = Alignment.CenterVertically) {
                OutlinedTextField(
                    modifier = Modifier.weight(1f),
                    value = code,
                    leadingIcon = {
                        Icon(Icons.Filled.Place, null)
                    },
                    placeholder = {
                        Text("验证码")
                    },
                    onValueChange = {
                        code = it
                    },
                    singleLine = true
                )
                AsyncImage(
                    modifier = Modifier.weight(0.618f).aspectRatio(16f / 6f).clickable(
                        onClick = onCodeClicked
                    ),
                    model = ImageRequest.Builder(LocalPlatformContext.current)
                        .data(imageUrl)
                        .crossfade(true)
                        .build(),
                    contentDescription = null,
                    contentScale = ContentScale.FillBounds
                )
            }

            var checked by remember { mutableStateOf(false) }
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.align(Alignment.Start)
            ) {
                Checkbox(
                    checked = checked,
                    onCheckedChange = { checked = it }
                )
                Text(
                    "记住密码", modifier = Modifier.clickable(
                        onClick = {
                            checked = !checked
                        }
                    )
                )
            }

            Button(
                modifier = Modifier.fillMaxWidth(),
                onClick = {
                    if (username.isEmpty()) {
                        showSnackbar("请输入账号")
                        return@Button
                    }
                    if (password.isEmpty()) {
                        showSnackbar("请输入密码")
                        return@Button
                    }

                    if (code.isEmpty() || imageUrl.isEmpty()) {
                        showSnackbar("请输入验证码")
                        return@Button
                    }
                    uiState.write {
                        putString("username", username)
                        putString("password", password)
                        putString("code", code)
                    }
                    onLoginClicked()
                }
            ) {
                Text("登录")
            }
            Text(
                "立即注册", modifier = Modifier.align(Alignment.Start).clickable(
                    onClick = toRegisterClicked
                )
            )
        }

        Text(
            "Copyright © 2018-2025 ruoyi.vip All Rights Reserved.",
            color = Color.White,
            modifier = Modifier.align(Alignment.BottomCenter).padding(20.dp),
            maxLines = 1,
            textAlign = TextAlign.Center
        )
    }
    //开始进入默认点击验证码
    if (imageUrl.isEmpty()) {
        uiState.write {
            putString("username", username)
            putString("password", password)
            putString("code", code)
        }
        onCodeClicked()
    }
}

@Preview(widthDp = 700, heightDp = 500)
@Composable
fun LoginScreenPreview() {
    LoginScreen(
        showSnackbar = {},
        uiState = savedState(),
        imageUrl = "",
        onLoginClicked = {},
        onCodeClicked = {},
        toRegisterClicked = {}
    )
}